// pages/mine_center/caips_appiontment/appiontment.js
const app = getApp();
var request = require("../../../utils/request.js");
var alert = require("../../../utils/wx_alert.js");
var userInfo = require('../../../utils/getuserinfo.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    next: "",
    weekdays: [

    ],
    days: [

    ],
    selected_day: -1,
    select_time: "false",
    shooting_time: [],
    first_date: "",
    show_have: true,
    access: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var pages = getCurrentPages() //获取加载的页面
    var currentPage = pages[pages.length - 1] //获取当前页面的对象
    var currentPage_t = pages[pages.length - 2] //上个页面
    console.log(currentPage_t)
    if (currentPage_t.route == "pages/mine_center/caips_shop/shop") {
      this.setData({
        next: "确定",
        page: currentPage_t.route
      })
    } else {
      this.bindGetUserInfo = userInfo.bindGetUserInfo;
      this.next_time = userInfo.next_time;
      app.getSetting(this);      
      this.setData({
        next: "下一步",
        page: currentPage_t.route,
      })
    }
    //快捷导航跳转
    this.turn = request.turn;
    this.close_modal = request.close_modal;

    var y = (new Date()).getFullYear();
    var m = (new Date()).getMonth() + 1;

    var data = {
      year: y,
      month: m,
      storeId: options.storeId
    }
    request.request(app.globalData.getScheduleTime, data, function(res) {
      console.log(res)
      if (res.code == 0) {
        that.setData({
          days: res.dateArray,
          shooting_time: res.list,
          first_date: y + "年" + m + "月",
          phone_call: options.phone ? options.phone : "0",
          sent_date: y + "-" + m,
          orderId: options.order_id,
          storeId: options.storeId,
          options: options
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  selected_day: function(e) {
    var index = e.currentTarget.dataset.index;
    var days = this.data.days;
    this.setData({
      selected_day: index,
      end_day: days[index].day,
      end_week: days[index].weekday
    })
  },
  select_time: function(e) {
    var index = e.currentTarget.dataset.index;
    var shooting_time = this.data.shooting_time;
    this.setData({
      select_time: index,
      end_time: shooting_time[index].time
    })
  },
  sure_click: function() {
    var selected_day = this.data.end_day;
    var select_time = this.data.select_time;
    var end_time = this.data.end_time;
    var that = this;
    // if (selected_day<0){
    //   alert.alert("请选择日期");
    //   return;
    // }
    // if (select_time == "false"){
    //   alert.alert("请选择时间");
    //   return;      
    // }
    // 日期

    // 下单
    var sent_date = "";
    if (selected_day && end_time) {

      sent_date = this.data.sent_date + "-" + selected_day + " " + end_time + ":00";
    }

    var send_order = wx.getStorageSync("send_order");
    var upgradeIds = [];
    var snId;
    for (var i in send_order) {
      send_order[i]["num"] = 1;

      if (send_order[i].selected == true) {
        upgradeIds.push(send_order[i].id)
      } else {
        snId = send_order[i].snId
      }

    }

    if (this.data.page =="pages/mine_center/caips_appiontment/appiontment"){
      request.request(app.globalData.setSchedule, {
        orderId: this.data.orderId,
        shootTime: sent_date
      }, function (res_data) {
        if (res_data.code == 0) {
          alert.msg(res_data.msg);
          setTimeout(function () {

            wx.redirectTo({
              url: '/pages/mine_center/caips_shop/shop?order_id=' + that.data.orderId,
            })

          }, 800)

        } else {

          alert.msg(res_data.msg);

        }
      })

    }
    if (that.data.page == "pages/phone_detail/phone_detail"){
      var order_data = {
        storeId: this.data.storeId,
        productId: wx.getStorageSync("productId"),
        snId: snId,
        upgradeIds: upgradeIds,
        shootTime: sent_date
      }
      
      console.log(order_data)
      request.request(app.globalData.addOrder, order_data, function (res) {
        console.log(res)
        if (res.code == 0) {
          if (sent_date){
            request.request(app.globalData.setSchedule, {
              orderId: res.orderEx.id,
              shootTime: sent_date
            }, function (res_data) {
              if (res_data.code == 0) {
                alert.msg(res_data.msg);
                setTimeout(function () {

                  wx.navigateTo({
                    url: '/pages/order_pay/order_pay?sent_date=' + sent_date + '&storeId=' + that.data.storeId,
                  })

                }, 800)
              } else {

                alert.msg(res.msg);

              }
            })
          }else{
            wx.navigateTo({
              url: '/pages/order_pay/order_pay?sent_date=' + sent_date + '&storeId=' + that.data.storeId,
            })            
          }


        }
        else if (res.code == 401){
          alert.alert("请先授权以便为您提供更优质的服务", function () {
            wx.redirectTo({
              url: "/pages/mine_center/modify_time/modify_time?phone=" + that.data.options.phone + '&order_id=' + that.data.options.order_id + '&storeId=' + that.data.options.storeId
            })
          });
        } 
        else {
          alert.alert(res.msg,function(){
            wx.navigateBack({
              
            })
          });

        }
      })      
    }








  },
  call_phone: function(e) {
    console.log(e)
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  }
})